package daniel.taotao.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import daniel.taotao.common.pojo.EasyUITreeNode;
import daniel.taotao.common.utils.TaotaoResult;
import daniel.taotao.content.service.ContentCategoryService;
import daniel.taotao.mapper.TbContentCategoryMapper;
import daniel.taotao.pojo.TbContentCategory;
import daniel.taotao.pojo.TbContentCategoryExample;
import daniel.taotao.pojo.TbContentCategoryExample.Criteria;
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {
	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;
	@Override
	public List<EasyUITreeNode> getContCataList(Long id) {
	    TbContentCategoryExample example=new TbContentCategoryExample();
	    Criteria criteria = example.createCriteria();
	    criteria.andParentIdEqualTo(id);
	    List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
	    List<EasyUITreeNode> result=new ArrayList<>();
	    for(TbContentCategory c:list){
	    	EasyUITreeNode e=new EasyUITreeNode();
	    	e.setId(c.getId());
	    	e.setText(c.getName());
	    	e.setState(c.getIsParent()?"closed":"open");
	    	result.add(e);
	    }
		return result;
	}
	@Override
	public TaotaoResult addContentCategory(Long parentId, String name) {
		TbContentCategory ct=new TbContentCategory();
		ct.setParentId(parentId);
		ct.setName(name);
		ct.setIsParent(false);
		ct.setSortOrder(1);
		ct.setStatus(1);
		ct.setCreated(new Date());
		ct.setUpdated(new Date());
		contentCategoryMapper.insert(ct);
		TbContentCategory category = contentCategoryMapper.selectByPrimaryKey(parentId);
		System.out.println(category+"---------------------------------------");
		if(!category.getIsParent()){
			category.setIsParent(true);
			contentCategoryMapper.updateByPrimaryKey(category);
		}
		
		return TaotaoResult.ok(ct);
	}
	@Override
	public void updateContentCategory(Long id, String name) {
		TbContentCategory category = contentCategoryMapper.selectByPrimaryKey(id);
		category.setName(name);
		contentCategoryMapper.updateByPrimaryKey(category);
	}
	@Override
	public void deleteContentCategory(Long id) {
		TbContentCategory category = contentCategoryMapper.selectByPrimaryKey(id);
		if(!category.getIsParent()){
			Long parentId=category.getParentId();
			contentCategoryMapper.deleteByPrimaryKey(id);
			TbContentCategoryExample ex=new TbContentCategoryExample();
			Criteria criteria = ex.createCriteria();
			criteria.andParentIdEqualTo(parentId);
			List<TbContentCategory> list = contentCategoryMapper.selectByExample(ex);
			if(list.size()<1){
				TbContentCategory category2 = contentCategoryMapper.selectByPrimaryKey(parentId);
				category2.setIsParent(false);
				contentCategoryMapper.updateByPrimaryKey(category2);
			}
		}
		if(category.getIsParent()){
			del(category);
			contentCategoryMapper.deleteByPrimaryKey(category.getId());
		}
	}
	public void del(TbContentCategory category){
		TbContentCategoryExample exa=new TbContentCategoryExample();
		Criteria criteria = exa.createCriteria();
		criteria.andParentIdEqualTo(category.getId());
		List<TbContentCategory> list = contentCategoryMapper.selectByExample(exa);
		for(TbContentCategory cc:list){
			if(!cc.getIsParent()){
				contentCategoryMapper.deleteByPrimaryKey(cc.getId());
			}else{
				del(cc);
			}
		}
		
	}

}
